package com.reach.redis.factory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;

import com.reach.redis.annotation.CacheEvict;
import com.reach.redis.annotation.Cacheable;
import com.reach.redis.annotation.EnableCaching;
import com.reach.redis.bo.CacheConfig;
import com.reach.redis.manager.ManagerCache;
import com.reach.redis.utils.GsonUtils;

public class RedisEnableCache implements  BeanPostProcessor {

	private static final Logger logger = LoggerFactory.getLogger(AbstractCacheFactory.class);
	

	private ManagerCache  managerCache 	 = new ManagerCache();

	
	private List<String> filterClass = Arrays.asList(new String[] {"java","org.springframework","org.mybatis","org.apache"});


	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(isExistClassName(bean)) {
			return bean;
		}
		Annotation  enableCaching = AnnotationUtils.findAnnotation(bean.getClass(),EnableCaching.class);
		if(enableCaching != null) {
			ManagerCache.enableCache = true;
		}
			
		return bean;
	}
	
	private boolean isExistClassName(Object method) {
		if(method != null) {
			for(String filter : filterClass) {
				if(method.getClass().getName().indexOf(filter) > -1) {
					return true;
				}
			}
		}
		return false;
		
	}


}